// See https://aka.ms/new-console-template for more information
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using SourceGeneratorApp.InterfaceGenerator;
namespace SourceGeneratorApp
{
    partial class Program
    {
        private static void Main(string[] args)
        {
            Console.ReadKey();
            //IBusinessCenter businessCenter = new BusinessCenter();
            //var result = businessCenter.Multiplication(1, 8);
            //Console.WriteLine($"计算结果：{result}");

            //IDataCenter dataCenter = new DataCenter();
            //var time = dataCenter.QueryTimeNow();
            //Console.WriteLine($"当前是{time.Year}年{time.Month}月{time.Day}日");

            #region Compilation构建
            //var test = CreateTestCompilation();
            //foreach (SyntaxTree sourceTree in test.SyntaxTrees)
            //{
            //    var model = test.GetSemanticModel(sourceTree);

            //    var rewriter = new TypeInferenceRewriter(model);

            //    var newSource = rewriter.Visit(sourceTree.GetRoot());

            //    if (newSource != sourceTree.GetRoot())
            //    {
            //        Console.WriteLine(newSource.ToFullString());
            //        File.WriteAllText(sourceTree.FilePath, newSource.ToFullString());
            //    }
            //}
            #endregion

        }

        static partial void HelloCode(string name);
        static partial void AdditionOperation(int num1, int num2);
        static partial void ArryOperation(List<int> ints);


        /// <summary>
        /// 将文件创建成节点
        /// </summary>
        /// <returns></returns>
        private static Compilation CreateTestCompilation()
        {
            String programPath = @"..\..\..\Program.cs";
            String programText = File.ReadAllText(programPath);
            var programTree =
                           CSharpSyntaxTree.ParseText(programText)
                                           .WithFilePath(programPath);

            String rewriterPath = @"..\..\..\SyntaxRewritersGenerator\TypeInferenceRewriter.cs";
            String rewriterText = File.ReadAllText(rewriterPath);
            var rewriterTree =
                           CSharpSyntaxTree.ParseText(rewriterText)
                                           .WithFilePath(rewriterPath);


            SyntaxTree[] sourceTrees = { programTree, rewriterTree };

            MetadataReference mscorlib =
                    MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            MetadataReference codeAnalysis =
                    MetadataReference.CreateFromFile(typeof(SyntaxTree).Assembly.Location);
            MetadataReference csharpCodeAnalysis =
                    MetadataReference.CreateFromFile(typeof(CSharpSyntaxTree).Assembly.Location);

            MetadataReference[] references = { mscorlib, codeAnalysis, csharpCodeAnalysis };

            return CSharpCompilation.Create("SourceGeneratorApp",
                                            sourceTrees,
                                            references,
                                            new CSharpCompilationOptions(
                                                    OutputKind.ConsoleApplication));
        }
    }
}

